Crate easy_switch

source ·
Expand description

A traditional C-style switch expression. In contrast to traditional match expressions, the switch! macro will test against expressions, not patterns. The API of this crate is tiny, with just a single item.

Internally, switch! just expands to a bunch of if-else expressions!

Example

use easy_switch::switch;

let num = 50;
let case1 = 20;
let case2 = 30;
let matching = switch! { num;
    case1 => "case1",
    case2 => "case2",
    25 + 25 => "unnamed case",
    _ => "default case",
};
assert_eq!("unnamed case", matching);

Multiple Conditions

Multiple conditions can be chained together with a comma.

use easy_switch::switch;

#[derive(PartialEq, Eq)]
struct Example {
    field: i32,
    field2: i32,
}

impl Example {
    pub fn new(field2: i32) -> Self {
        Self {
            field: 10,
            field2,
        }
    }
}

let switchable = Example::new(10);
let result = switch! { switchable;
    Example::new(50), 50 == 50 => 50,
    Example::new(20), 50 == 50, 20 == 20 => 20,
    _ => 0,
};
assert_eq!(0, result);

Macros

  • The macro used to emulate switch statements.